﻿<%@ Page Language="C#" %>

<%@ Register Assembly="DevIsMagic.ExtendedListBox" Namespace="DevIsMagic.ExtendedListBox"
    TagPrefix="dim" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Linq" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager runat="server" />
    <div>
        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" AutoPostBack="True"
            OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"></asp:ListBox>
        <dim:ListBoxExtenderControl ID="ListBox1_ListBoxExtenderControl" runat="server" TargetControlID="ListBox1" />
        <br />
        <asp:PlaceHolder ID="phdSamples" runat="server"></asp:PlaceHolder>
    </div>
    </form>
</body>
</html>

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack) return;

        ListBox1.DataSource = from ConsoleColor conColor in Enum.GetValues(typeof(ConsoleColor))
                              let color = Color.FromName(conColor.ToString())
                              orderby color.GetBrightness()
                              select new { Name = color.Name, Hex = color.ToArgb().ToString("X") };
        ListBox1.DataValueField = "Hex";
        ListBox1.DataTextField = "Name";
        ListBox1.DataBind();
    }

    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var result = from ListItem li in ListBox1.Items
                     where li.Selected
                     select new Label { Text = li.Text, ForeColor = Color.FromArgb(int.Parse(li.Value, System.Globalization.NumberStyles.HexNumber)) };

        foreach (var lbl in result)
        {
            phdSamples.Controls.Add(lbl);
            phdSamples.Controls.Add(new LiteralControl("&nbsp;"));
        }
    }
</script>
